//
//  CZMainTabBarVC.m
//  XinLang_OC
//
//  Created by WONG on 16/2/20.
//  Copyright © 2016年 cheuhui. All rights reserved.
//

#import "CZMainTabBarVC.h"
#import "CZHomeVC.h"
#import "CZMessageVC.h"
#import "CZDiscoverVC.h"
//#import "CZProfileVC.h"
#import "CZComposeVC.h"

@interface CZMainTabBarVC ()

/**
 *  tabBar中间加号按钮
 */
@property (nonatomic,strong) UIButton *composeButton;

@end

@implementation CZMainTabBarVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    CZHomeVC *homeVC = [CZHomeVC new];
    [self addChildViewController:homeVC title:@"首页" imageName:@"tabbar_home"];
    
    CZMessageVC *messageVC = [CZMessageVC new];
    [self addChildViewController:messageVC title:@"消息" imageName:@"tabbar_message_center"];
    
    // 占位控制器
    
    [self addChildViewController:[UIViewController new]];
    
    CZDiscoverVC *discoverVC = [CZDiscoverVC new];
    [self addChildViewController:discoverVC title:@"发现" imageName:@"tabbar_discover"];
    
//    CZProfileVC *profileVC = [CZProfileVC new];
    UIViewController *profileVC = [UIViewController new];
    [self addChildViewController:profileVC title:@"我" imageName:@"tabbar_profile"];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.composeButton.size = self.composeButton.currentBackgroundImage.size;
    // 为避免点到 占位控制器，所以按钮高度加大一点
    self.composeButton.height = self.composeButton.height + 5;
    self.composeButton.centerX = self.tabBar.width * 0.5;
    self.composeButton.centerY = self.tabBar.height * 0.5;
    
}

- (void)addChildViewController:(UIViewController *)childController title:(NSString *)title imageName:(NSString *)imageName {
    childController.title = title;
    childController.tabBarItem.image = [UIImage imageNamed:imageName];
    // 让选中的图片使用原来的颜色，不要系统自动渲染
    childController.tabBarItem.selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"%@_highlighted",imageName]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    // 设置标题颜色
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[NSForegroundColorAttributeName] = [UIColor orangeColor];
    [childController.tabBarItem setTitleTextAttributes:dic forState:UIControlStateSelected];
    
    [self addChildViewController:[[UINavigationController alloc] initWithRootViewController:childController]];
}


- (void)composeClick {
    CZComposeVC *composeVC = [CZComposeVC new];
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:composeVC];
    
    [self presentViewController:nav animated:YES completion:nil];
}

#pragma mark - lazy load

- (UIButton *)composeButton {
    if (!_composeButton) {
        _composeButton = [[UIButton alloc] init];
        
        [_composeButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [_composeButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        
        [_composeButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [_composeButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        
        [self.tabBar addSubview:_composeButton];
        
        [_composeButton addTarget:self action:@selector(composeClick) forControlEvents:UIControlEventTouchUpInside];

    }
    return _composeButton;
}


@end



















